package cn.openea.perpartner.meily.web.service.impl;

import cn.openea.perpartner.meily.web.entity.BlackList;
import cn.openea.perpartner.meily.web.entity.vo.BlackListVo;
import cn.openea.perpartner.meily.web.mapper.BlackListMapper;
import cn.openea.perpartner.meily.web.service.IBlackListService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("blackListService")
public class BlackListServiceImpl extends ServiceImpl<BlackListMapper, BlackList> implements IBlackListService {

    @Override
    public BlackList selectOneByUserId(String userId) {
        LambdaQueryWrapper<BlackList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BlackList::getMember, userId);
        queryWrapper.last("limit 1");
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<BlackList> findListByVo(BlackListVo vo) {
        LambdaQueryWrapper<BlackList> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(vo.getMember())) {
            queryWrapper.eq(BlackList::getMember, vo.getMember());
        }
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public void add(BlackList blackList) {
        // 先判断是否已经存在
        if (selectOneByUserId(blackList.getMember()) != null) {
            return;
        }
        baseMapper.insert(blackList);
    }

    /**
     * 根据用户id与类型，将用户移出黑名单
     * @param blackList
     * @return
     */
    @Override
    public boolean removeByUserId(BlackList blackList) {
        LambdaQueryWrapper<BlackList> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(blackList.getMember())) {
            queryWrapper.eq(BlackList::getMember, blackList.getMember());
        }
        if (StringUtils.isNotBlank(blackList.getType())) {
            queryWrapper.eq(BlackList::getType, blackList.getType());
        }
        return remove(queryWrapper);
    }
}